package com.zlx.controller;

import com.alibaba.fastjson.JSONObject;
import com.zlx.constants.Constant;
import com.zlx.entities.User;
import com.zlx.service.UserService;
import com.zlx.utils.MdUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.beans.Encoder;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @Author 周罗校
 * @Description
 * @ClassName LoginServlet
 * @date 2020.09.22 11:40:17
 * 该版权归周罗校所有，最终解释权以官方为准
 */

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {
    private UserService userService = new UserService();
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("login");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remeber = request.getParameter("remeber");
        String checkCode = request.getParameter("checkCode");
        HttpSession session = request.getSession();
        if (checkCode.equalsIgnoreCase((String) session.getAttribute(Constant.CHECKCODE_PIC))) {
            //获取登陆情况
            User loginUser = userService.checkLogin(username, MdUtil.md5(password));
            //七天免登陆逻辑
            if ("1".equals(remeber)) {
                String s = JSONObject.toJSONString(loginUser);
                String encode = URLEncoder.encode(s, "utf-8");
                Cookie cookie = new Cookie(Constant.LOGIN_COOKIE, encode);
                cookie.setMaxAge(7 * 24 * 60 * 6);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            if (loginUser == null) {
                //密码或用户名错误
                request.setAttribute(Constant.ERROR_MSG, "用户名或密码错误！");
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            } else {
                //登陆成功
                session = request.getSession();
                session.setAttribute(Constant.LOGIN_USER, loginUser);
                response.sendRedirect("/user/list");
            }
        }else {
            request.setAttribute(Constant.ERROR_MSG, "验证码错误！");
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
    }
    /**
     * @Author 周罗校
     * @Description 忘记密码
     * @Param [request, response]
     * @return void
     * @Date 2020.09.27 13:35:47
     **/
    protected void forget(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String checkCode = request.getParameter("checkCode");
        HttpSession session = request.getSession();
        String code= (String) session.getAttribute(Constant.EMAIL_CODE);
        //验证码正确
        if (checkCode!=null&&checkCode.equals(code)&&code!=null){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            User user = new User();
            user.setUsername(username);
            user.setPassword(MdUtil.md5(password));
            userService.updatePasswordByUsername(user);
            response.sendRedirect("/index.jsp");
        }else {
            request.setAttribute(Constant.ERROR_MSG,"验证码错误!");
            request.getRequestDispatcher("/forget.jsp").forward(request,response);
        }
    }
}
